package ca.cbc.gis.services;

import ca.cbc.giscommon.dao.*;
import ca.cbc.giscommon.entity.*;
import ca.cbc.giscommon.util.StringUtil;
import com.vividsolutions.jts.geom.Geometry;

import java.util.ArrayList;
import java.util.List;

public class GISService {
    private StoryDAO storyDAO;
    private SourceDAO sourceDAO;
    private CategoryDAO categoryDAO;
    private ContentDAO contentDAO;
    private ZoneDAO zoneDAO;
    private ShapeDAO shapeDAO;
    private EventDAO eventDAO;
    private TweetDAO tweetDAO;
    private TwitterUserDAO twitterUserDAO;
    private EventSubcategoryDAO eventTypeDAO;

    public GISService() {
        storyDAO = new StoryDAO();
        sourceDAO = new SourceDAO();
        categoryDAO = new CategoryDAO();
        contentDAO = new ContentDAO();
        zoneDAO = new ZoneDAO();
        shapeDAO = new ShapeDAO();
        eventDAO = new EventDAO();
        tweetDAO = new TweetDAO();
        twitterUserDAO = new TwitterUserDAO();
        eventTypeDAO = new EventSubcategoryDAO();
    }

    public List<Story> getStories(String zoneIds, Geometry bbox, String categoryIds, long startTime, long endTime, String orderBy, boolean isAscending, int limit, int offset, boolean includeAll) {
        return storyDAO.listWithZone(zoneIds, bbox, categoryIds, startTime, endTime, orderBy, isAscending, limit, offset, includeAll);
    }

    public List<Content> getContents(String zoneIds, Geometry bbox, String categoryIds, long startTime, long endTime, String orderBy, boolean isAscending, int limit, int offset) {
        return contentDAO.listWithZone(zoneIds, bbox, categoryIds, startTime, endTime, orderBy, isAscending, limit, offset);
    }

    public List<Shape> getShapes(String zoneIds, Geometry bbox, String shapeTypeIds, String categoryIds, String orderBy, boolean isAscending, int limit, int offset) {
        return shapeDAO.list(zoneIds, bbox, shapeTypeIds, categoryIds, orderBy, isAscending, limit, offset);
    }

    public List<Event> getEvents(String zoneIds, Geometry bbox, String categoryIds, String eventTypeIds, long startTime, long endTime, String orderBy, boolean isAscending, int limit, int offset) {
        return eventDAO.listWithZone(zoneIds, bbox, categoryIds, eventTypeIds, startTime, endTime, orderBy, isAscending, limit, offset);
    }

    public List<Tweet> getTweets(String zoneIds, Geometry bbox, String categoryIds, long startTime, long endTime, String orderBy, boolean isAscending, int limit, int offset) {
        return tweetDAO.listWithZone(zoneIds, bbox, categoryIds, startTime, endTime, orderBy, isAscending, limit, offset);
    }

    public List<Zone> getZones() {
        return zoneDAO.list();
    }

    public List<Source> getSources() {
        return sourceDAO.list();
    }

    public List<Category> getCategories() {
        return categoryDAO.list();
    }

    public List<TwitterUser> getTwitterUsers() {
        return twitterUserDAO.list();
    }

    public List<EventSubcategory> getEventSubcategories() {
        return eventTypeDAO.list();
    }

    public List<Geometry> getZoneGeometries(String ids) {
        if (StringUtil.instance.isNullOrEmpty(ids)) {
            return null;
        }

        List<Geometry> geometries = new ArrayList<Geometry>();
        List<Zone> zones = zoneDAO.listByIds(ids);
        for (Zone zone : zones) {
            geometries.add(zone.getTheGeom());
        }

        if (geometries.isEmpty()) {
            return null;
        }

        return geometries;
    }
}
